import { _decorator, Component, Label, Node, Sprite, Vec3 } from 'cc';
import { UIMgr } from '../Framework/Managers/UIMgr';
import { HttpUtil } from '../Framework/Managers/HttpUtil';
import { UICtrl } from '../Framework/Managers/UICtrl';
import { Globle } from '../Framework/Utils/Globle';
import { ResMgr } from '../Framework/Managers/ResMgr';
const { ccclass, property } = _decorator;

@ccclass('Ranking')
export class Ranking extends UICtrl {
    private rankingData = [];
    @property(Node)
    private lastWeekContent:Node;
    @property(Node)
    private lastWeekScrollView:Node;
    @property(Node)
    private thisWeekScrollView:Node;
    content:Node;
    @property(Label)
    private nikeName;
    @property(Label)
    private score;
    @property(Label)
    private rankLabel;
    @property(Sprite)
    private rankImg ;
    @property(Sprite)
    private avatar ;
    @property(Node)
    private lastWeekChooseMask:Node;
    @property(Node)
    private thisWeekChooseMask:Node;
    onLoad(): void {
        super.onLoad();
        this.content = this.getChildOrComponent("bg/ScrollView/view/content");
    }
    
    start() {
        HttpUtil.get("/api/getRank",{roomId:0},(state,data)=>{
            this.ranking(state,data);
        });
        this.personalData( Globle.loginData.lastWeekRank)
    }
    ranking(bool,data){
        console.log(this.content);
        console.log("data",data)
        this.rankingData = data;
        if(data.thisWeek!=null&&data.thisWeek.length>0){
            for(let i = 1;i<=data.thisWeek.length;i++){
                let ranking_box = UIMgr.Instance.CreatePrefab("RankingBox",new Vec3(0,(i-1)*-200-68,0),this.content);
                ranking_box.init(data.thisWeek[i-1].rank,data.thisWeek[i-1].avatar,data.thisWeek[i-1].nickName,data.thisWeek[i-1].score);
            }
        }
        if(data.lastWeek!=null&&data.lastWeek.length>0){
            for(let i = 1;i<=data.lastWeek.length;i++){
                let ranking_box = UIMgr.Instance.CreatePrefab("RankingBox",new Vec3(0,(i-1)*-200-68,0),this.lastWeekContent);
                ranking_box.init(data.thisWeek[i-1].rank,data.thisWeek[i-1].avatar,data.thisWeek[i-1].nickName,data.thisWeek[i-1].score);
            }
        }
        
        
        this.lastWeekScrollView.active = false;
        this.lastWeekChooseMask.active = false;
    }


    update(deltaTime: number) {

    }
    clickReturn() {
        console.log("12312313")
       this.node.destroy()
    }
    personalData(rank){
        this.nikeName.getComponent(Label).string = Globle.userData.nickName+"";
        // this.avatar.getComponent(Label).string = Globle.userData.avatar+"";
        if(rank == -1){
            this.rankLabel.node.active = true;
            this.rankImg.node.active = false;
            this.rankLabel.getComponent(Label).string = "未上榜";
        }else if(rank == 1){
            this.rankLabel.node.active = false;
            this.rankImg.node.active = true;
            this.rankImg.spriteFrame = ResMgr.Instance.getAsset("GUI","GameUI/Ranking/ranking_goldmedal/spriteFrame");
        }else if(rank == 2){
            this.rankLabel.node.active = false;
            this.rankImg.node.active = true;
            this.rankImg.spriteFrame = ResMgr.Instance.getAsset("GUI","GameUI/Ranking/ranking_silver_medal/spriteFrame");
        }else if(rank == 3){
            this.rankLabel.node.active = false;
            this.rankImg.node.active = true;
            this.rankImg.spriteFrame = ResMgr.Instance.getAsset("GUI","GameUI/Ranking/ranking_bronze/spriteFrame");
        }else{
            this.rankLabel.getComponent(Label).string = rank+"";
        }
    }
    clickWeekRanking(){
        this.personalData( Globle.loginData.thisWeekRank);
        this.thisWeekScrollView.active = true;
        this.thisWeekChooseMask.active = true;
        this.lastWeekScrollView.active = false;
        this.lastWeekChooseMask.active = false;
    }
    clickLastWeek(){
        this.personalData( Globle.loginData.lastWeekRank);
        this.thisWeekScrollView.active = false;
        this.thisWeekChooseMask.active = false;
        this.lastWeekScrollView.active = true;
        this.lastWeekChooseMask.active = true;
    }

}


